<template>
  <div id="pictureManage">
    <!--搜索框-->
    <a-form layout="inline" :model="searchParam" @finish="doSearch" class="searchForm">
      <a-form-item label="关键词">
        <a-input v-model:value="searchParam.searchText" placeholder="请输入关键词" allowClear />
      </a-form-item>
      <a-form-item label="类型">
        <a-input v-model:value="searchParam.category" placeholder="类型" allowClear />
      </a-form-item>
      <a-form-item label="标签">
        <a-input v-model:value="searchParam.tag" placeholder="标签" allowClear />
      </a-form-item>
      <a-form-item label="审核状态">
        <a-select
          v-model:value="searchParam.reviewStatus"
          placeholder="请选择审核状态"
          :options="PICTURE_REVIEW_STATUS_OPTIONS"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <!--表格-->
    <a-table
      :columns="columns"
      :data-source="pictureData"
      bordered
      :pagination="pagination"
      @change="pageSizeChange"
    >
      <template #bodyCell="{ column, record }">
        <!--图片展示方式-->
        <template v-if="column.dataIndex === 'url'">
          <a-image :src="record.url" :width="200" :height="200" />
        </template>
        <!--标签展示方式-->
        <template v-if="column.dataIndex === 'tags'">
          <a-space wrap>
            <a-tag v-for="tag in JSON.parse(record.tags || '[]')" :key="tag">{{ tag }}</a-tag>
          </a-space>
        </template>
        <template v-if="column.dataIndex === 'picInfo'">
          <div>格式：{{ record.picFormat }}</div>
          <div>宽度：{{ record.picWidth }}</div>
          <div>高度：{{ record.picHeight }}</div>
          <div>宽高比：{{ record.picScale }}</div>
          <div>大小：{{ (record.picSize / 1024).toFixed(2) }}KB</div>
        </template>
        <template v-if="column.dataIndex === 'reviewInfo'">
          <div>审核状态：{{ getReviewStatusByCode(record.reviewStatus)?.desc }}</div>
          <div v-if="record.reviewMsg">审核信息：{{ record.reviewMsg }}</div>
          <div v-if="record.reviewerId">审核人id：{{ record.reviewerId }}</div>
          <div v-if="record.reviewTime">审核时间：{{ record.reviewTime }}</div>
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space wrap>
            <a-button
              v-if="record.reviewStatus !== PASS.status"
              type="primary"
              size="middle"
              @click="handlePictureReview(record, PASS.status)"
            >
              通过
            </a-button>
            <a-button
              v-if="record.reviewStatus !== REJECT.status"
              danger
              size="middle"
              @click="handlePictureReview(record, REJECT.status)"
            >
              拒绝
            </a-button>
            <a-button type="link" size="middle" :href="`/add_pic?id=${record.id}`" target="_blank">
              编辑
            </a-button>
            <a-button danger size="middle" @click="doDelete(record.id)">删除</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import {
  deletePictureByIdUsingDelete,
  listPictureUsingGet,
  reviewPictureUsingPost,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import {
  getReviewStatusByCode,
  PASS,
  PICTURE_REVIEW_STATUS_OPTIONS,
  REJECT,
} from '@/constants/picture.ts'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '图片',
    dataIndex: 'url',
  },
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '简介',
    dataIndex: 'introduction',
    ellipsis: true,
  },
  {
    title: '类型',
    dataIndex: 'category',
  },
  {
    title: '标签',
    dataIndex: 'tags',
  },
  {
    title: '图片信息',
    dataIndex: 'picInfo',
  },
  {
    title: '用户id',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '审核信息',
    dataIndex: 'reviewInfo',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]
// 图片列表
const pictureData = ref<API.Picture[]>([])
const total = ref(0)
// 搜索条件
const searchParam = reactive<API.listPictureUsingGETParams>({
  pageNum: 1,
  pageSize: 20,
  name: '',
  introduction: '',
  category: '',
  tags: [],
  picFormat: '',
})

// 获取图片数据
const queryPicturePage = async () => {
  const res = await listPictureUsingGet({
    ...searchParam,
    public: truez,
  })
  pictureData.value = res.data.records ?? []
  total.value = res.data.total ?? 0
}
// 分页器
const pageSizeOptions = ref<string[]>(['1', '2', '10', '20', '50'])
const pageSizeChange = (page) => {
  searchParam.pageNum = page.current
  searchParam.pageSize = page.pageSize
  queryPicturePage()
}
const pagination = computed(() => {
  return {
    current: searchParam.pageNum,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true, // 显示可改变分页大小
    showTotal: (total: number) => `共${total}条`,
    pageSizeOptions: pageSizeOptions.value,
  }
})
const doSearch = () => {
  // 重置页码
  searchParam.pageNum = 1
  queryPicturePage()
}
const doDelete = async (id: string) => {
  if (id) {
    await deletePictureByIdUsingDelete({ pictureId: id })
    message.success('操作成功')
    queryPicturePage()
  }
}
const handlePictureReview = async (picture: API.Picture, status: number) => {
  const res = await reviewPictureUsingPost({
    id: picture.id,
    reviewStatus: status,
    reviewMsg: status === PASS.status ? '管理员操作通过' : '管理员操作拒绝',
  })
  message.success(res.msg)
  queryPicturePage()
}
onMounted(() => {
  queryPicturePage()
})
</script>

<style scoped>
.searchForm {
  margin-bottom: 16px;
}
</style>
